import Vue from "vue";

/**
 * 金额转换（分->元）
 * 100 => 1
 * @param {number} num
 */
export function moneyFormatter(num) {
	return (isNaN(num) ? 0.00 : parseFloat((num / 100).toFixed(2)))
}

/**
 * 时间转换成 如 1分钟前等
 * @param {number} gmt
 */
export function timeFormatter(gmt) {
	if (gmt == undefined || gmt == '') {
		return ''
	}
	gmt = gmt.replace(/-/g,'/')
	/* 获取当前时间时间戳毫秒 */
	var current = new Date().getTime()
	/* 根据时间字符串转时间戳毫秒 */
	var time = new Date(gmt).getTime()
	let value = (current - time) / 1000
	if (value < 110) {
		return '刚刚'
	}else if(value <= 3600) {
		return parseInt(value / 60) + '分前'
	}else if(value <= 86400){
		return parseInt(value / 3600) + '小时前'
	}else {
		var now = new Date(gmt)
		var year = now.getFullYear()
		var month = now.getMonth() + 1
		if (month < 10) {
			month = '0' + month
		}
		
		var date = now.getDate()
		if (date < 10) {
			date = '0' + date
		}
		
		var hours = now.getHours()
		if (hours < 10) {
			hours = '0' + hours
		}
		
		var minutes = now.getMinutes()
		if (minutes < 10) {
			minutes = '0' + minutes
		}

		var atYear = new Date().getFullYear()
		if (year = atYear) {
			return month + '-' + date + ' ' + hours + ':' + minutes
		}
		return year + '-' + month + '-' + date + ' ' + hours + ':' + minutes
	}
	return ''
}